<?php

include 'include/db.php';

$filename_tmp = $file_tmp . "export_" . (date("d_m_Y_H_i_s",time())) . ".txt";

$fp = fopen($filename_tmp, "w");

//$res = mysql_query("SELECT * FROM $table");
$res = mysql_query($sql);
$line = "TemplateType=HomeImprovement\tVersion=1.4\tThis row for Amazon.com use only.  Do not modify or delete.";
$line .= "\n";
fputs($fp, $line);
$line = "sku\tstandard-product-id\tproduct-id-type\ttitle\tbrand\tmanufacturer\tmfr-part-number\tmerchant-catalog-number\tbullet-point1\tbullet-point2\tbullet-point3\tbullet-point4\tbullet-point5\tdescription\tproduct_type\titem-price\tcurrency\tcondition-type\tcondition-note\tquantity\tlaunch-date\trelease-date\tleadtime-to-ship\trestock-date\tmax-aggregate-ship-quantity\tproduct-tax-code\tsale-price\tsale-start-date\tsale-end-date\tsearch-terms1\tsearch-terms2\tsearch-terms3\tsearch-terms4\tsearch-terms5\tplatinum-keywords1\tplatinum-keywords2\tplatinum-keywords3\tplatinum-keywords4\tplatinum-keywords5\trecommended-browse-node1\trecommended-browse-node2\tmain-image-url\tswatch-image-url\tother-image-url1\tother-image-url2\tother-image-url3\tother-image-url4\tother-image-url5\tother-image-url6\tother-image-url7\tother-image-url8\tparentage\tparent-sku\trelationship-type\tvariation-theme\tsize\tcolor\tmaterial\tbattery-capacity\tpower-source\tvoltage\twattage\titem-package-quantity\titem-weight-unit-of-measure\titem-weight\titem-length-unit-of-measure\titem-length\titem-height\titem-width\tdisplay-volume\tdisplay-volume-unit-of-measure\tdisplay-weight\tdisplay-weight-unit-of-measure\tdisplay-length\tdisplay-length-unit-of-measure\tshipping-weight\tshipping-weight-unit-of-measure\tfulfillment-center-id\tpackage-height\tpackage-width\tpackage-length\tpackage-length-unit-of-measure\tpackage-weight\tpackage-weight-unit-of-measure\tgrit-rating\tis-gift-message-available\tis-giftwrap-available\tis-discontinued-by-manufacturer\tregistered-parameter\tlegal-disclaimer\tupdate-delete";
$line .= "\n";
fputs($fp, $line);

// remove the result pointer back to the start
mysql_data_seek($res, 0);

// and loop through the actual data
while($row = mysql_fetch_assoc($res)) {
    //prepare sku
    $sku = $row["products_model"];
        //check if we use EAN or ASIN (prefer EAN)
        if ($row["products_ASIN"]!="")
                {
                $product_id_type = "ASIN";
                $product_id = $row["products_ASIN"];
        }
        else
        {
        $product_id_type = "ean";
        $product_id = $row["products_ean"];
        $product_id = preg_replace('/\W/','',$product_id);
        $ean_chek_result = Validate_ISPN::ean13($product_id);
        if ($ean_chek_result != 1 ){
                $mail_message = "the EAN [". $product_id . "] in article with sku: " . $sku . " is not valid" ;
                $mailer = Mail::factory('smtp',$mail_options);
                if (true === PEAR::isError($mailer))
                {
                   die ($mailer->getMessage());
                }
                $mail_res = $mailer->send($mail_receipient,$mail_headers,$mail_message);
                if (true === PEAR::isError($mail_res))
                {
                   die ($mail_res->getMessage());
                }
                //echo $mail_message . "\n";
        }
        }
        //prepare products_name
        $products_name = $row["products_name"];
        //check $row[3] => Hersteller
    if ($row["manufacturers_name"] == "")
                {
                $manufacturer = "EWH";
                }
    else $manufacturer = $row["manufacturers_name"];

        //remove "\t" and "\r\n" from description
        $string = $row["products_description"];
        $string = str_replace("\t",'',$string);
        $string = str_replace("\r\n",'',$string);
        $products_description = $string;

        //errechne VK_Preis und runde auf 2 Nachkommastellen
        $price = round(($row["products_price"] * $tax * $price_factor), 2);

        //check $row[6] => Menge auf Lager
    if ($row["products_quantity"] == "")
                {
                $quantity = "0";
                }
    elseif ($row["products_quantity"] < 0)
        {
                $quantity = "10";
                }
    else $quantity = $row["products_quantity"];

        //build Image URL for Amazon
        $product_image = $osc_images . $row["products_image"];

        //leadtime to Ship
        if ($row["products_leadtime_to_ship"] == "")
                {
                $leadtime_to_ship = "3";
        }
        else $leadtime_to_ship = $row["products_leadtime_to_ship"];

        //set ams Brows Nodes (Kategorien)
                        $ams_browse_node1 = $row["ams_browse_node1"];
        $ams_browse_node2 = $row["ams_browse_node2"];

        //set product_weight
        $product_weight = $row["products_weight"];

        //search Entrys
        $products_search1 = $row["products_search1"];
        $products_search2 = $row["products_search2"];
        $products_search3 = $row["products_search3"];
        $products_search4 = $row["products_search4"];
        $products_search5 = $row["products_search5"];

        $line = $sku . "\t" .
                        $product_id . "\t" .
                        $product_id_type . "\t" .
                        $products_name . "\t" .
                        $manufacturer . "\t" .
                        $manufacturer . "\t\t\t\t\t\t\t\t" .
                        $products_description . "\tHardware\t" .
                        $price . "\t\t\t\t" .
                        $quantity ."\t\t\t" .
                        $leadtime_to_ship . "\t\t\t\t\t\t\t" .
                        $products_search1 . "\t" .
                        $products_search2 . "\t" .
                        $products_search3 . "\t" .
                        $products_search4 . "\t" .
                        $products_search5 . "\t\t\t\t\t\t" .
                        $ams_browse_node1 . "\t" .
                        $ams_browse_node2 . "\t" .
                        $product_image . "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" .
                        $item_weight_unit_of_measure . "\t" .
                        $product_weight . "\t";

    $line .= "\n";
    $line = convertToWindowsCharset($line);
    fputs($fp, $line);

}

fclose($fp);
//copy temp File to Exportfolder
rename($filename_tmp, $amtu_folder . "outgoing/" . basename($filename_tmp));
?>